<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/10/29
 * Time: 16:32
 */

namespace App\Game;

use Carbon\Carbon;
use App\Game\Traits\SgTrait;
use App\Models\OddsHandicap;
use App\Models\Def\MainGamePlat;

class sgcp extends Game
{
    use SgTrait;

    private $platName = 'sgcp';

    const API_PLAYER_GET_BALANCE = '/api/account';
    const API_PLAYER_GET_JOIN_GAME = '/api/login';
    const API_PLAYER_JOIN_PC_GAME = '/member/index?_OLID_=';
    const API_PLAYER_JOIN_MOBILE_GAME = '/mobile/member/index?_OLID_=';
    const API_PLAYER_KICK = '/api/logout';
    const API_PLAYER_TRANSACTION_IN = '/api/transaction';
    const API_PLAYER_CONFIRM = '/api/confirm';
    const API_PLAYER_BET_RECORD = '/api/bets';

    const API_PLAYER_CHECK_TRANSFER = '/Transaction/CheckTransferStatus';


    public function __construct()
    {
        $this->header = [
            "Accept" => "application/json",
            "Content-Type" => "application/json",
        ];
        $this->adds = [
            'main_game_plat_code' => $this->platName,
            'plat_type' => 'lottery',
            'status' => 1,
        ];
    }

    public function getLength()
    {
        return $this->length;
    }

    public function getPre()
    {
        return $this->pre;
    }

    /**
     * gameapi
     * 2018/9/19 16:15
     * Administrator
     * __get
     *
     * @param string $key
     * @return mixed
     */
    public function __get($key)
    {
        return $this->$key;
    }

    public function __set($key, $value)
    {
        $this->$key = $value;
    }


    /**
     * gameapi
     * 2018/9/24 14:16
     * Administrator
     * curlCreateMember
     * 创建用户
     * @param $accountUserName
     * @param $password
     * @param $request
     * @return array
     */
    public function curlCreateMember($accountUserName, $password, $request)
    {
        return ['success' => true, 'data' => ['accountUserName' => strtolower($accountUserName), 'password' => $password], 'message' => ''];
    }

    /**
     * gameapi
     * 2018/9/24 14:15
     * Administrator
     * curlGetBalance
     * 查询余额
     * @param $accountUserName
     * @return array
     */
    public function curlGetBalance($accountUserName)
    {
        $data = [
            'agentID' => $this->agentID,
            'username' => $accountUserName,
        ];

        $output = $this->sgRequest('GET', $this->gameUrl . self::API_PLAYER_GET_BALANCE, $data);
        $output = json_decode($output, true);
        if ($output['success'] == true) {
            return ['success' => true, 'data' => ['balance' => $output['balance']], 'message' => ''];
        } else {
            return ['success' => false, 'data' => [], 'message' => '查询余额失败，错误原因：' . $output['message']];
        }
    }


    //转入游戏
    public function curlTransferIn($accountUserName, $price, $transferid)
    {
        $data = [
            'agentID' => $this->agentID,
            'amount' => $price,
            'force' => true,
            'testing' => 0,
            'unique' => $transferid,
            'username' => $accountUserName,
        ];
        $output = $this->sgRequest('GET', $this->gameUrl . self::API_PLAYER_TRANSACTION_IN, $data);
//        file_put_contents('/home/gameapi/storage/logs/test.log', 'sgsgsg.111..'.$output.PHP_EOL, FILE_APPEND);
        $output = json_decode($output, true);

        if ($output['success'] == true) {
            //确认订单
            $data = [
                'agentID' => $this->agentID,
                'id' => $output['id'],
            ];
            $output = $this->sgRequest('GET', $this->gameUrl . self::API_PLAYER_CONFIRM, $data);
            $output = json_decode($output, true);
            if ($output['success'] == true){
                return ['success' => true, 'data' => ['transferid' => $transferid, 'transfer_state' => 1], 'message' => ''];
            }else{
                return ['success' => false, 'data' => ['transfer_state' => 0], 'message' => '确认转帐失败，错误原因：' . $output['message']];
            }
        } else {
            return ['success' => false, 'data' => ['transfer_state' => 0], 'message' => '转帐失败，错误原因：' . $output['message']];
        }
    }

    //转出游戏
    public function curlTransferTo($accountUserName, $price, $transferid)
    {
        return $this->curlTransferIn($accountUserName, -$price, $transferid);
    }

    //查询订单状态
    public function curlCheckTransfer($request)
    {
        $json = [
            'json' => [
                'MerchantCode' => $this->merchantCode,
                'PlayerId' => $request->accountUserName,
                'ProductWallet' => $this->productWallet,
                'TransactionId' => $request->transferid,

            ]
        ];

        $output = $this->request('POST', $this->gameUrl . self::API_PLAYER_CHECK_TRANSFER, [], [], $json);
        $output = json_decode($output, true);

        if (array_key_exists('Code', $output)) {
            if ($output['Code'] == 0 && $output['Message'] == 'Successful.') {
                return ['success' => true, 'data' => ['transfer_state' => 1], 'message' => ''];
            }elseif($output['Code'] == 541){
                return ['success' => false, 'data' => ['transfer_state' => 0], 'message' => '转账失败' . $output['Message']];
            } else {
                return ['success' => false, 'data' => ['transfer_state' => -1], 'message' => '检查转账失败，错误原因：' . $output['Message']];
            }
        } else {
            return ['success' => false, 'data' => ['transfer_state' => -1], 'message' => '请求平台失败，状态未知'];
        }
    }

    //踢线
    public function curlKick($accountUserName)
    {
        $data = [
            'agentID' => $this->agentID,
            'username' => $accountUserName,
        ];
        $output = $this->sgRequest('POST', $this->gameUrl . self::API_PLAYER_KICK, $data);
        $output = json_decode($output, true);
        if ($output['success'] == true) {
            return ['success' => true, 'data' => [], 'message' => '成功'];
        } else {
            return ['success' => false, 'data' => [], 'message' => '删除玩家会话失败，错误原因：' . $output['message']];
        }
    }

    /**
     * gameapi
     * 2018/9/24 16:35
     * Administrator
     * curlJoinGame
     * 进入游戏
     * @param $request
     * @return array
     */
    public function curlJoinGame($request)
    {
        $data = [
            'agentID' => $this->agentID,
            'username' => $request->accountUserName,
            'testing' => 0,
        ];
        $output = $this->sgRequest('GET', $this->gameUrl . self::API_PLAYER_GET_JOIN_GAME, $data);
        $output = json_decode($output, true);

        if ($output['success'] == true) {
            return ['success' => true, 'data' => ['url' => $this->joinGame . self::API_PLAYER_JOIN_PC_GAME.$output['session']], 'message' => '成功'];
        } else {
            return ['success' => false, 'data' => [], 'message' => '进入游戏失败，错误原因：' . $output['message']];
        }

    }


    /**
     * gameapi
     * 2018/9/24 16:35
     * Administrator
     * curlJoinMobileGame
     * 手机端进入游戏
     * @param $request
     * @return array
     */
    public function curlJoinMobileGame($request)
    {
        $data = [
            'agentID' => $this->agentID,
            'username' => $request->accountUserName,
            'testing' => 0,
        ];
        $output = $this->sgRequest('GET', $this->gameUrl . self::API_PLAYER_GET_JOIN_GAME, $data);
        $output = json_decode($output, true);

        if ($output['success'] == true) {
            return ['success' => true, 'data' => ['url' => $this->joinGame . self::API_PLAYER_JOIN_MOBILE_GAME.$output['session']], 'message' => '成功'];
        } else {
            return ['success' => false, 'data' => [], 'message' => '进入游戏失败，错误原因：' . $output['message']];
        }
    }


    //游戏抓单
    public function curlGetBetRecord(Carbon $startTime, Carbon $endTime)
    {
//        file_put_contents('/home/gameapi/storage/logs/test.log', 'sgsgsg.aaa..'.$startTime->toDateTimeString().PHP_EOL, FILE_APPEND);
        $data = [];
        $page = 1;
        $datas = [
            'agentID' => $this->agentID,
            'settledTime' => $startTime->toDateTimeString(),
        ];
        $totalPage = 0;
        do {
            $datas['page'] = $page;
            $output = $this->sgRequest('GET', $this->gameUrl . self::API_PLAYER_BET_RECORD, $datas);
            $output = json_decode($output, true);
            if ($output['success'] == true) {
                $totalPage = $output['total'];
                if (count($output['list']) >= 1) {
                    $data = array_merge($data, $output['list']);
                }
            }
            $page++;
        } while ($page <= $totalPage);
//        file_put_contents('/home/gameapi/storage/logs/test.log', 'sgsgsg.ooo..'.json_encode($data).PHP_EOL, FILE_APPEND);
//        return[];
        return $data;
    }
}